let app = getApp();
Page({
  data: {
    imagePath: '',
    idCardImageUrl: '',
    selectedCategories: '',
    userInfo: null,
    id: null,
    serviceCategoryIds: []
  },
  onLoad(options) {
    this.getData()
    let ids = options.ids
    if (ids) {
      this.setData({
        serviceCategoryIds: ids
      })
    }
  },
  onShow() {
    this.getData()
    if (this.data.serviceCategoryIds) {

    }
  },
  bindNameInput(e) {
    let name = this.data.userInfo;
    name.mName = e.detail.value;
    this.setData({
      userInfo: name
    });
  },
  bindPhoneInput(e) {
    let phone = this.data.userInfo;
    phone.mPhone = e.detail.value;
    this.setData({
      userInfo: phone
    });
  },
  bindAddressInput(e) {
    let address = this.data.userInfo;
    address.mCommonaddress = e.detail.value;
    this.setData({
      userInfo: address
    });
  },
  bindDetailAddressInput(e) {
    let detailAddress = this.data.userInfo;
    detailAddress.mAddress = e.detail.value;
    this.setData({
      userInfo: detailAddress
    });
  },
  bindCertificateInput(e) {
    let certificate = this.data.userInfo;
    certificate.mCerid = e.detail.value;
    this.setData({
      userInfo: certificate
    });
  },
  saveData() {
    wx.showLoading({
      title: '正在保存...'
    });
    console.log(this.data.userInfo.mName);
    console.log(this.data.userInfo.mCommonaddress);
    wx.request({
      url: 'http://localhost:8080/master/master',
      method: "PUT",
      data: {
        "id": this.data.userInfo.id,
        "mName": this.data.userInfo.mName,
        "mPhone": this.data.userInfo.mPhone,
        "mCommonaddress": this.data.userInfo.mCommonaddress,
        "mAddress": this.data.userInfo.mAddress,
        "mCerid": this.data.userInfo.mCerid,
        "mImage": this.data.imagePath,
      },
      success(res) {
        if (res.data.code === 200) {
          wx.showToast({
            title: '保存成功',
            icon: 'success'
          });
        } else {
          wx.showToast({
            title: '保存失败，请重试',
            icon: 'none'
          });
        }
      },
      fail(err) {
        console.error('保存失败:', err);
        wx.showToast({
          title: '保存失败',
          icon: 'none'
        });
      },
    });
  },
  chooseImage() {
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        console.log(res);
        this.setData({
          imagePath: res.tempFilePaths[0]
        });
        this.uploadImage(res.tempFilePaths[0]);
      }
    });
  },
  uploadImage(imagePath) {
    wx.uploadFile({
      url: 'http://localhost:8080/common/upload',
      data: {
        file: imagePath,
      },
      filePath: imagePath,
      name: 'file',
      success: (res) => {
        console.log(res.data);
        wx.showToast({
          title: '上传成功',
          icon: 'success',
          duration: 2000
        });
        const result = JSON.parse(res.data);
        if (result.code === 200) {
          this.setData({
            imagePath: result.url
          });
        }
      },
      fail: (res) => {
        console.log(res);
        wx.showToast({
          title: '上传失败',
          icon: 'error',
          duration: 2000
        });
      },
      complete() {
        wx.hideLoading();
      }
    });
  },
  getData() {
    let app = getApp();
    let id = app.globalData.userID;
    wx.request({
      url: 'http://localhost:8080/master/master/' + id,
      method: "GET",
      success: (res) => {
        console.log(res);
        let userInfo = res.data.data;
        if (userInfo && userInfo.serviceOneTypes.length > 0) {
          userInfo.serviceOneTypesTitle = userInfo.serviceOneTypes[0].title;
        }
        this.setData({
          userInfo: userInfo
        });
      }
    });
  }
});